package com.isoftstone.controller;

import com.isoftstone.pojo.Job;
import com.isoftstone.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
public class JobController {

    @Autowired
    private JobService jobService;

    // 处理应聘信息的操作请求
    // URL为单层的时候可以不用加前面的/
    @RequestMapping(value = "/modifyjob.do", method = {RequestMethod.POST, RequestMethod.GET})
    public String modifyJob(Job job, Model model, @RequestParam("action") String action) {

        System.out.println(action);
        // 当前操作为添加应聘信息
        if ("addjob".equals(action)) {
            job.setCreatetime(new Date());
            int ok = jobService.add(job);
            // 跳转到应聘信息查看的页面
            if (ok != 0) {
                return "forward:/job.do?action=listjob";
            } else {
                model.addAttribute("message", "插入失败");
                return "/WEB-INF//user/error";
            }
        }
        // 当前操作为删除应聘信息
        if("deletejob".equals(action))
        {
            Job job1 = jobService.queryById(job.getId());
            jobService.deleteById(job.getId());
            List<Job> list = jobService.queryByIsstock(job1.getIsstock());
            model.addAttribute("list", list);
            String isstock = job1.getIsstock() ? "yes" : "no";
            return "redirect:job.do?action=listjob&isstock=" + isstock;
        }

        // 当前操作为更新应聘信息,将应聘人员入库
        if("updatejob".equals(action)){
            int updated = jobService.updateByJob(job, job.getId());
            if(updated != 0){
                return "redirect:/job.do?action=listjob&isstock=yes";
            }
            else{
                model.addAttribute("message", "更新失败");
                return "/WEB-INF//user/error";
            }
        }
        model.addAttribute("message", "操作失败");
        return "/WEB-INF//user/error";
    }

    // 显示应聘人员的详细信息
    @RequestMapping(value = "/detailjob.do", method = RequestMethod.GET)
    public String detailJob(@RequestParam("id") Integer id, Model model) {
        Job detailJob = jobService.queryById(id);
        model.addAttribute("job", detailJob);
        return "detailjob";
    }

    // 跳转到应聘信息录入页面
    @RequestMapping("/addjob.do")
    public String addjob() {
        return "addjob";
    }

    // 跳转的应聘信息查看页面
    @RequestMapping("/job.do")
    public String showJob(Model model,
                          @RequestParam(value = "isstock", required = false,defaultValue = "no")String isstock,
                          @RequestParam("action")String action) {
        List<Job> jobs;
        System.out.println(isstock);
        if("yes".equals(isstock)){
             jobs = jobService.queryByIsstock(true);
        }
        else {
             jobs = jobService.queryByIsstock(false);
        }
        model.addAttribute("list", jobs);
            return "listjob";
    }
}
